package org.jxc.service;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.ProductTypeDAO;
import org.jxc.domain.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class ProductTypeService implements IService {

	private ProductTypeDAO productTypeDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		productTypeDAO = (ProductTypeDAO) DAOFactory.getDAO(sessionFactory,
				ProductTypeDAO.class);
	}

	/**
	 * 分页
	 * 
	 * @param page
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public Page<ProductType> listPagination(Page<ProductType> page,
			String superId) throws CRUDException {
		if (Validate.notNull(superId))
			return productTypeDAO.pagination(page,
					"from ProductType where deleteFlag='0' and superId = ?", null, superId);
		else
			return productTypeDAO.pagination(page,
					"from ProductType where deleteFlag='0'", null);
	}

	/**
	 * 获取所有分类
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<ProductType> findAllProductType(String orderBy)
			throws CRUDException {
		List<ProductType> list = productTypeDAO
				.find("from ProductType where deleteFlag='0' order by " + orderBy);
		return list;
	}

	/**
	 * (overload)获取所有分类,按默认规则(创建时间)排序
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<ProductType> findAllProductType() throws CRUDException {
		return findAllProductType("createTime");
	}

	/**
	 * 根据id查询分类
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public ProductType findById(String id) throws Exception {
		ProductType productType = productTypeDAO.get(id);
		return productType;
	}

	/**
	 * 新建分类
	 * 
	 * @param product
	 * @throws CRUDException
	 */
	public void saveProductType(ProductType productType) throws CRUDException {
		// 设置编号
		String code = productTypeDAO.findNextCode(productType.getSuperId());
		productType.setDeleteFlag("0");
		productType.setCode(code);
		productType.setCreateTime(new Date());
		productType.setCreator(LoginBean.getLoginBean().getUser());
		productTypeDAO.save(productType);
	}

	/**
	 * 修改分类
	 * 
	 * @param product
	 * @throws CRUDException
	 */
	public void updateProductType(ProductType productType) throws CRUDException {
		ProductType oldProductType = productTypeDAO.get(productType.getId());
		oldProductType.setName(productType.getName());
		oldProductType.setSuperId(productType.getSuperId());
		oldProductType.setEditor(LoginBean.getLoginBean().getUser());
		oldProductType.setEditTime(new Date());
		productTypeDAO.update(oldProductType);
	}

	/**
	 * 批量删除分类
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteProductType(String id) throws CRUDException {
		ProductType productType = productTypeDAO.get(id);
		productType.setDeleteFlag("1");
		productTypeDAO.update(productType);
	}

}